/*!
 * New Launch.
 * Date: 2015/12/29.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg) {
  $(function () {
    var $dataFrm = $('#data-frm'),
      $submitBtn = $dataFrm.find('button[type="submit"]'),
      dataFieldRule, options, dataValidator;
    // console.log($dataFrm);
    dataFieldRule = {
      list_name:{
        rule:{
            required:{
                message:VAM.j('List name cannot be empty')
            }
        },
        messageContainer:'list_name_err'
      },
      domain_list: {
        rule: {
          required: {
            message: VAM.j('Domain list cannot be empty')
          }
        },
        messageContainer: 'domain_list_err'
      }
    };
    options = {
      submitOnSucceed: false,
      onvalidate: function(evt) {
        if (evt.resultList.length <= 0) {
            $dataFrm.ajaxSubmit({
                beforeSubmit: function() {
                  $submitBtn.button('loading');
                },
                success: function (data) {
                  $submitBtn.button('reset');
                  if (data.status === 'success') {
                    VamOpMsg.messager(data);
                    window.location.href = 'black-white-list';
                  } else {
                    VamOpMsg.messager(data);
                  }
                }
            });
        };
      }
    };
    formValidator.validate($dataFrm, dataFieldRule, options);
  });
});
